﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ATMService
{
    public class ConnectDB
    {
        ManageATMDataContext mnATM;
        public ConnectDB()
        {
            mnATM = new ManageATMDataContext();
        }
        public Account GetAccount(string Ip)
        {
            Account acc = null;
            var query = from account in mnATM.Accounts
                        where account.Ip == Ip
                        select new
                        {
                            Ip = account.Ip,
                            Pin = account.Pin,
                            Name = account.Name,
                            TimeCreate = account.TimeCreate,
                            Balance = account.Balance
                        };
            foreach (var account in query)
            {
                acc = new Account
                {
                    Ip = account.Ip,
                    Pin = account.Pin,
                    Name = account.Name,
                    TimeCreate = account.TimeCreate,
                    Balance = Convert.ToInt32(account.Balance)
                };
            }

            return acc;
        }

        public bool ExceptMoney(int money, string Ip)
        {
            try
            {
                Account acc = mnATM.Accounts.Single(s => s.Ip == Ip);
                acc.Balance = acc.Balance - money;
                mnATM.SubmitChanges();
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }
        public bool AddMoney(int money, string Ip)
        {
            try
            {
                Account acc = mnATM.Accounts.Single(s => s.Ip == Ip);
                acc.Balance = acc.Balance + money;
                mnATM.SubmitChanges();
            }
            catch (Exception e)
            {
                return false;
            }


            return true;
        }
        public bool SendingAccounts(Transaction trs)
        {
            Transaction sendingaccount = new Transaction
            {
                IpFrom = trs.IpFrom,
                IpTo = trs.IpTo,
                Type = trs.Type,
                Amount = trs.Amount,
                Balance = trs.Balance,
                Description = trs.Description,
                time = trs.time
            };

            mnATM.Transactions.InsertOnSubmit(sendingaccount);
            mnATM.SubmitChanges();
            return true;
        }

        public List<Transaction> GetHistoryTransaction(string ip)
        {
            List<Transaction> lstHistory = new List<Transaction>();
            var query = from trs in mnATM.Transactions
                        where trs.IpFrom == ip
                        select new
                        {
                            ID = trs.id,
                            IpFrom = trs.IpFrom,
                            IpTo = trs.IpTo,
                            Type = trs.Type,
                            Amount = trs.Amount,
                            Balance = trs.Balance,
                            Description = trs.Description,
                            time = trs.time
                        };
            foreach (var transaction in query)
            {
                Transaction t = new Transaction
                {
                    id = transaction.ID,
                    IpFrom = transaction.IpFrom,
                    IpTo = transaction.IpTo,
                    Type = transaction.Type,
                    Amount = transaction.Amount,
                    Balance = transaction.Balance,
                    Description = transaction.Description,
                    time = transaction.time
                };
                lstHistory.Add(t);
            }
            return lstHistory;
        }

        //public List<Transaction> GetReceivedTransfer(string Ip)
        //{
        //    List<Transaction> listSa = new List<Transaction>();
        //    var query = from trs in mnATM.Transactions
        //                where (trs.IpTo == Ip)
        //                select new
        //                {
        //                    ID = trs.id,
        //                    IpFrom = trs.IpFrom,
        //                    IpTo = trs.IpTo,
        //                    Type = trs.Type,
        //                    Amount = trs.Amount,
        //                    Balance = trs.Balance,
        //                    Description = trs.Description,
        //                    time = trs.time
        //                };
        //    foreach (var sendingAccount in query)
        //    {
        //        Transaction sending = new Transaction
        //        {
        //            id = sendingAccount.ID,
        //            IpFrom = sendingAccount.IpFrom,
        //            IpTo = sendingAccount.IpTo,
        //            Type = sendingAccount.Type,
        //            Amount = sendingAccount.Amount,
        //            Balance = sendingAccount.Balance,
        //            Description = sendingAccount.Description,
        //            time = sendingAccount.time
        //        };
        //        listSa.Add(sending);
        //    }

        //    return listSa;
        //}
        //public List<Transaction> GetSendTransfer(string Ip)
        //{

        //    List<Transaction> listSa = new List<Transaction>();
        //    var query = from sendingAccount in mnATM.Transactions
        //                where ((sendingAccount.IpFrom == Ip) && (sendingAccount.IpTo != null))
        //                select new
        //                {
        //                    ID = sendingAccount.id,
        //                    IpFrom = sendingAccount.IpFrom,
        //                    IpTo = sendingAccount.IpTo,
        //                    Type = sendingAccount.Type,
        //                    Amount = sendingAccount.Amount,
        //                    Balance = sendingAccount.Balance,
        //                    Description = sendingAccount.Description,
        //                    time = sendingAccount.time
        //                };
        //    foreach (var sendingAccount in query)
        //    {
        //        Transaction sending = new Transaction
        //        {
        //            id = sendingAccount.ID,
        //            IpFrom = sendingAccount.IpFrom,
        //            IpTo = sendingAccount.IpTo,
        //            Type = sendingAccount.Type,
        //            Amount = sendingAccount.Amount,
        //            Balance = sendingAccount.Balance,
        //            Description = sendingAccount.Description,
        //            time = sendingAccount.time
        //        };
        //        listSa.Add(sending);
        //    }

        //    return listSa;
        //}
        //public List<Transaction> WithdrawMoney(string Ip)
        //{
        //    List<Transaction> listSa = new List<Transaction>();
        //    var query = from sendingAccount in mnATM.Transactions
        //                where (sendingAccount.IpFrom == Ip && sendingAccount.IpTo == null)
        //                select new
        //                {
        //                    ID = sendingAccount.id,
        //                    IpFrom = sendingAccount.IpFrom,
        //                    IpTo = sendingAccount.IpTo,
        //                    Type = sendingAccount.Type,
        //                    Amount = sendingAccount.Amount,
        //                    Balance = sendingAccount.Balance,
        //                    Description = sendingAccount.Description,
        //                    time = sendingAccount.time
        //                };
        //    foreach (var trs in query)
        //    {

        //        Transaction sending = new Transaction
        //        {
        //            id = trs.ID,
        //            IpFrom = trs.IpFrom,
        //            IpTo = trs.IpTo,
        //            Type = trs.Type,
        //            Amount = trs.Amount,
        //            Balance = trs.Balance,
        //            Description = trs.Description,
        //            time = trs.time
        //        };
        //        listSa.Add(sending);
        //    }

        //    return listSa;

        //}
    }
}